<template>
	<view class="single-list">
	   <slot name="items" v-bind:list="list"></slot>	
	</view>
</template>

<script>
	const TAG_NAME = "SINGLE_LIST";
	export default {
		props: {
			//请求数据
			sendData:{
				type:Object,
				default:()=>({})
			},
			//请求地址
			url:{
				type:String,
				default:''
			}
		},
		data() {
			return {
				loadingType:'more',
				pageNum:1,
				pageSize:20,
				list:[],
			}
		},
		watch:{
			/*onLoad内必须在this.$nextTick更改*/
			sendData:{
				handler(v,o){
					// console.log(TAG_NAME,'sendData',v,o);
					this.getList(true);
				},
				immediate:false,	//由父组件触发，因为可能onShow刷新
				deep:true,
			}
		},
		methods:{
			//搜索
			async getList(isRefresh=false){
				// console.log('触发函数getList')
				if(isRefresh){
					this.loadingType='more'
					this.pageNum=1
					this.list=[]
				}
				if (this.loadingType === 'loading' || this.loadingType == 'noMore') {
					//防止重复加载
					return;
				}
				// console.log('触发开始',this.pageNum)
				this.loadingType = 'loading';
				const sendData={
					page: this.pageNum,
					listrow: this.pageSize,
					...this.sendData,
				}
				let res = await uni.$request(this.url,sendData);
				uni.stopPullDownRefresh();
				// this.isTriggerRefresh=false;
				// this.isLoaded = true;
				let result = res.data || [];
				
				if ( this.pageSize > result.length) {
					this.loadingType = 'noMore';
				} else {
					this.loadingType = 'more';
				}
				// 页数加一
				this.pageNum++;
				if(isRefresh){
					this.pageNum = 2;
					this.list=result;
					this.$emit('reqEnd',{loadingType:this.loadingType,pageOneRes:result}) //主要判断list是否空
					return;
				}
				result.forEach(item => {
					// item = Object.assign(item, this.handleStatus(item.status));
					this.list.push(item);
				});
				this.$emit('reqEnd',{loadingType:this.loadingType,pageOneRes:this.list[0]})
			},
		}

	}
</script>

<style lang="scss" scoped>
	// .submit-btn{
	// 	opacity: 0.6;
	// }
</style>
